﻿
namespace Interscape.TwilioClient.Components
{
    /// <summary>
    /// A utility class containing functions that format URLs for the different Twilio endpoints.
    /// </summary>
    /// <remarks>While it is possible to call these functions directly, it is better to call them through the Urls property on the Manager classes.</remarks>
    public class ServiceUrls
    {

        #region Private members

        static string _accountSid;
        static string baseUrl = "https://api.twilio.com/2008-08-01";
        private static string _accountBase = "{0}/Accounts/{1}";
        private static string _callsBase = "{0}/Calls/{1}";
        private static string _incomingNumbersBase = "{0}/IncomingPhoneNumbers/{1}";
        private static string _notificationsBase = "{0}/Notifications/{1}";
        private static string _outgoingCallerIdsBase = "{0}/OutgoingCallerIds/{1}";
        private static string _recordingsBase = "{0}/Recordings/{1}";
        private static string _transcriptionsBase = "{0}/Transcriptions/{1}";

        #endregion

        #region Constructors

        public ServiceUrls()
        {
        }

        public ServiceUrls(string accountSid)
        {
            _accountSid = accountSid;
        }

        #endregion

        //AccountManager URLs
        public string AccountUrl(bool appendBase) { return string.Format(_accountBase, (appendBase ? baseUrl : ""), _accountSid); }

        //CallManager URLs
        public string CallsUrl(bool appendBase) { return string.Format(_callsBase, AccountUrl(appendBase), ""); }
        public string CallInstanceUrl(bool appendBase, string instanceSid) { return string.Format(_callsBase, AccountUrl(appendBase), instanceSid); }

        //IncomingPhoneNumberManager URLs
        public string IncomingPhoneNumbersUrl(bool appendBase) { return string.Format(_incomingNumbersBase, AccountUrl(appendBase), ""); }
        public string IncomingPhoneNumberInstanceUrl(bool appendBase, string instanceSid) { return string.Format(_incomingNumbersBase, AccountUrl(appendBase), instanceSid); }
        public string IncomingPhoneNumberLocalUrl(bool appendBase) { return string.Format(_incomingNumbersBase, AccountUrl(appendBase), "Local"); }
        public string IncomingPhoneNumberTollFreeUrl(bool appendBase) { return string.Format(_incomingNumbersBase, AccountUrl(appendBase), "TollFree"); }

        //NotificationManager URLs
        public string NotificationsUrl(bool appendBase) { return string.Format(_notificationsBase, AccountUrl(appendBase), ""); }
        public string NotificationInstanceUrl(bool appendBase, string instanceSid) { return string.Format(_notificationsBase, AccountUrl(appendBase), instanceSid); }
        
        //OutgoingCallerIdManager URLs
        public string OutgoingCallerIdsUrl(bool appendBase) { return string.Format(_outgoingCallerIdsBase, AccountUrl(appendBase), ""); }
        public string OutgoingCallerIdInstanceUrl(bool appendBase, string instanceSid) { return string.Format(_outgoingCallerIdsBase, AccountUrl(appendBase), instanceSid); }

        //RecordingManager URLs
        public string RecordingsUrl(bool appendBase) { return string.Format(_recordingsBase, AccountUrl(appendBase), ""); }
        public string RecordingInstanceUrl(bool appendBase, string instanceSid) { return string.Format(_recordingsBase, AccountUrl(appendBase), instanceSid + ".xml"); }
        public string RecordingMp3Url(bool appendBase, string instanceSid) { return string.Format(_recordingsBase, AccountUrl(appendBase), instanceSid + ".mp3"); }
        public string RecordingWavUrl(bool appendBase, string instanceSid) { return string.Format(_recordingsBase, AccountUrl(appendBase), instanceSid + ".wav"); }

        //TranscriptionManager URLs
        public string TranscriptionsUrl(bool appendBase) { return string.Format(_transcriptionsBase, AccountUrl(appendBase), ""); }
        public string TranscriptionInstanceUrl(bool appendBase, string instanceSid) { return string.Format(_transcriptionsBase, AccountUrl(appendBase), instanceSid); }
        public string TranscriptionInstanceTxtUrl(bool appendBase, string instanceSid) { return string.Format(_transcriptionsBase, AccountUrl(appendBase), instanceSid + ".txt"); }

    }
}
